import { User } from 'oidc-client'
import Vue from 'vue'
import { createOidcAuth, SignInType, LogLevel } from '../vue-oidc-client'

const loco = window.location
const appRootUrl = `${loco.protocol}//${loco.host}${process.env.BASE_URL}`

const authority = "http://127.0.0.1:8080";
const idsrvAuth = createOidcAuth(
  'main',
  SignInType.Window,
  appRootUrl,
  {
    authority: authority,
    client_id: 'vue-client', // 'implicit.shortlived',
    response_type: 'code',
    scope: 'openid profile',
    // test use
    prompt: 'login',
    metadata : {
      "issuer":  "http://127.0.0.1:8080",
      "authorization_endpoint":`${authority}/oauth2/authorize`,
      "jwks_uri": `${authority}/oauth2/jwks`,
      "userinfo_endpoint":`${authority}/userinfo`,
      "end_session_endpoint": `${authority}/logout`,
      "token_endpoint":`${authority}/oauth2/token`
    }
  },
  console,
  LogLevel.Debug
)

// handle events
idsrvAuth.events.addAccessTokenExpiring(function() {
  // eslint-disable-next-line no-console
  console.log('access token expiring')
})

idsrvAuth.events.addAccessTokenExpired(function() {
  // eslint-disable-next-line no-console
  console.log('access token expired')
})

idsrvAuth.events.addSilentRenewError(function(err: Error) {
  // eslint-disable-next-line no-console
  console.error('silent renew error', err)
})

idsrvAuth.events.addUserLoaded(function(user: User) {
  // eslint-disable-next-line no-console
  console.log('user loaded', user)
})

idsrvAuth.events.addUserUnloaded(function() {
  // eslint-disable-next-line no-console
  console.log('user unloaded')
})

idsrvAuth.events.addUserSignedOut(function() {
  // eslint-disable-next-line no-console
  console.log('user signed out')
})

idsrvAuth.events.addUserSessionChanged(function() {
  // eslint-disable-next-line no-console
  console.log('user session changed')
})


// a little something extra
Vue.prototype.$oidc = idsrvAuth

export default idsrvAuth
